package com.wumai.modernguilib.component;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.util.text.ITextComponent;
import org.lwjgl.glfw.GLFW;

public abstract class Button extends TextComponent implements IClickable {
    boolean isPressed = false;
    protected int backgroundColor = 0x80000000; // 默认半透明黑色背景
    protected int backgroundColorHover = 0xA0202020; // 默认悬停时的背景色
    protected int borderColor = 0x00000000; // 默认白色边框
    protected int borderColorHover = 0x00000000; // 默认悬停时绿色边框
    protected boolean isHovered = false;

    public Button(String text) {
        super(text);
        // 设置默认的按钮样式
        this.backgroundColor = 0x80000000;
        this.backgroundColorHover = 0xA0202020;
        this.borderColor = 0xFFFFFFFF;
        this.borderColorHover = 0xFF75ff1e;
    }

    public Button(int x, int y, int width, int height, String text) {
        super(x, y, width, height, text);
        // 设置默认的按钮样式
        this.backgroundColor = 0x80000000;
        this.backgroundColorHover = 0xA0202020;
        this.borderColor = 0xFFFFFFFF;
        this.borderColorHover = 0xFF00FF00;
    }

    public Button(ITextComponent text) {
        super(text);
    }

    public Button(int x, int y, int width, int height, ITextComponent text) {
        super(x, y, width, height, text);
        // 设置默认的按钮样式
        this.backgroundColor = 0x80000000;
        this.backgroundColorHover = 0xA0202020;
        this.borderColor = 0xFFFFFFFF;
        this.borderColorHover = 0xFF00FF00;
    }

    @Override
    protected void onBeforeRender(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        // 更新悬停状态
        isHovered = isMouseOver(mouseX, mouseY);

        // 确定当前使用的颜色
        int currentBackgroundColor = isPressed ? backgroundColorHover : (isHovered ? backgroundColorHover : backgroundColor);
        int currentBorderColor = isHovered ? borderColorHover : borderColor;

        // 设置背景和边框颜色
        this.setBackgroundColor(currentBackgroundColor);
        this.setBorderColor(currentBorderColor);
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        if (!visible || !enabled) {
            return false;
        }
        if (isMouseOver(mouseX, mouseY) && button == GLFW.GLFW_MOUSE_BUTTON_1) {
            isPressed = true;
            return true;
        }
        return false;
    }

    /**
     * 鼠标释放事件处理方法，将事件传递给子组件
     * @param mouseX 鼠标X坐标(NDC)
     * @param mouseY 鼠标Y坐标(NDC)
     * @param button 鼠标按键
     * @return 是否处理了该事件
     */
    @Override
    public boolean mouseReleased(double mouseX, double mouseY, int button) {
        if (!visible || !enabled) {
            return false;
        }
        if (isMouseOver(mouseX, mouseY) && button == GLFW.GLFW_MOUSE_BUTTON_1 && isPressed) {
            isPressed = false;
            onClick();
            return true;
        }
        return false;
    }

    // 设置悬停时背景颜色
    public void setBackgroundColorHover(int backgroundColorHover) {
        this.backgroundColorHover = backgroundColorHover;
    }

    // 设置悬停时边框颜色
    public void setBorderColorHover(int borderColorHover) {
        this.borderColorHover = borderColorHover;
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public int getBackgroundColorHover() {
        return backgroundColorHover;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public int getBorderColorHover() {
        return borderColorHover;
    }
}